//
//  MTCardListHeaderView.m
//  met
//
//  Created by 李晓杰 on 2019/5/13.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTCardListHeaderView.h"

@implementation MTCardListHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.contentView];
        [self addSubview:self.locImageView];
        [self addSubview:self.delImageView];
        [self addSubview:self.nameLabel];
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).with.offset(14);
            make.left.mas_equalTo(self).with.offset(19);
            make.bottom.mas_equalTo(self).with.offset(-14);
            make.width.mas_equalTo(107);
        }];
        
        [self.locImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView).with.offset(5);
            make.left.mas_equalTo(self.contentView).with.offset(10);
            make.bottom.mas_equalTo(self.contentView).with.offset(-5);
            make.width.mas_equalTo(15);
        }];
        
        [self.delImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView).with.offset(5);
            make.bottom.mas_equalTo(self.contentView).with.offset(-5);
            make.right.mas_equalTo(self.contentView).with.offset(-7);
            make.width.mas_equalTo(18);
        }];
        //10 + 15 + 9 + width + 9 + 18 +7 = 68 + width
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.locImageView.mas_right).with.offset(9);
            make.top.bottom.mas_equalTo(self.contentView);
            make.right.mas_equalTo(self.delImageView.mas_left).with.offset(-9);
        }];
        
        
        
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *loc = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locAddress)];
        [self addGestureRecognizer:loc];
    }
    return self;
}


- (void)setName:(NSString *)name{
    _name = name;
    _nameLabel.text = _name;
    
    CGRect rect = [_name boundingRectWithSize:CGSizeMake(MAXFLOAT, 28) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:CLRudeCircleTextFont(15)} context:nil];
    CGFloat width = rect.size.width;
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width + 70);
    }];
}








- (UIView *)contentView{
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.frame = self.bounds;
        _contentView.backgroundColor = KBlackColor;
        _contentView.layer.cornerRadius = 14;
        _contentView.layer.masksToBounds = YES;
    }
    return _contentView;
}
- (UIImageView *)locImageView{
    if (_locImageView == nil) {
        _locImageView = [[UIImageView alloc] init];
        _locImageView.image = ImageNamed(@"发现 拷贝");
       
    }
    return _locImageView;
}
- (UIImageView *)delImageView{
    if (_delImageView == nil) {
        _delImageView = [[UIImageView alloc] init];
        _delImageView.image = ImageNamed(@"anniu_guanbi");
        
        _delImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *del = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteAddress)];
        [_delImageView addGestureRecognizer:del];
    }
    return _delImageView;
}
- (UILabel *)nameLabel{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"";
        _nameLabel.textColor = KWhiteColor;
        _nameLabel.font = CLRudeCircleTextFont(15);
    }
    return _nameLabel;
}
- (void)locAddress {
    if (self.nameLabel.text.length > 0) {
        return;
    }
    if (self.addAddress) {
        self.addAddress(@"1");
    }
}
- (void)deleteAddress {
    if (self.delAddress) {
        self.delAddress(@"1");
    }
}
@end
